package com.hutu.core.aspect;

import com.hutu.model.resp.Response;
import com.hutu.util.RobotUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.stream.Collectors;


/**
 * @author yangyf
 * @version 1.0
 */
@Slf4j
//@Aspect
//@Component
public class SysAspect {

    @Autowired
    private RobotUtil robotUtil;


    /**
     * 拦截范围
     * @param proceed
     * @return
     * @throws Throwable
     */
    @Around("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    public Object aroundGet(ProceedingJoinPoint proceed) throws Throwable {
        return run(proceed);
    }

    @Around("@annotation(org.springframework.web.bind.annotation.PostMapping)")
    public Object aroundSave(ProceedingJoinPoint proceed) throws Throwable {
        return run(proceed);
    }

    @Around("@annotation(org.springframework.web.bind.annotation.PutMapping)")
    public Object aroundUpdate(ProceedingJoinPoint proceed) throws Throwable {
        return run(proceed);
    }

    @Around("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
    public Object aroundDelete(ProceedingJoinPoint proceed) throws Throwable {
        return run(proceed);
    }

    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object aroundRequest(ProceedingJoinPoint proceed) throws Throwable {
        return run(proceed);
    }


    /**
     * 数据请求入口
     */
    private Object run(ProceedingJoinPoint proceed) throws Throwable {
        try {
            return proceed.proceed();
        } catch (Exception e) {
            e.printStackTrace();
            String err = "请求参数：" + Arrays.stream(proceed.getArgs()).collect(Collectors.toList());
            err += "\r\n【监控到系统异常】" + getErrorMsg(e);
            //robotUtil.sendMsg(err);
            log.error(err);
            return Response.error(500,e.getMessage());
        }
    }


    /**
     * 拼接错误信息
     * @param e
     * @return
     */
    public static String getErrorMsg(Throwable e) {
        // 错误类名
        String exceptionClass = e.getClass().getName();
        String exceptionClassName = exceptionClass.substring(exceptionClass.lastIndexOf(".") + 1, exceptionClass.length());
        // 详细错误信息
        StringBuffer errorDesc = new StringBuffer();
        errorDesc.append("\r\n异常类: " + exceptionClass + "\r\n详细错误内容:\r\n");
        if (e.getStackTrace() != null) {
            StringBuffer errMsg = new StringBuffer();
            Arrays.stream(e.getStackTrace()).forEach(i -> errMsg.append(i.toString() + "\r\n"));
            String s = errMsg.toString().split("\\)")[0] + ")";
            errorDesc.append(e.getMessage() + "\r\n" + s);

        }
        return errorDesc.toString() + "请以下人员及时跟进。";
    }

}



